import React from 'react';
import ReactDOM from 'react-dom';

class Clock extends React.Component {
    constructor(props) {
        super(props);
        this.state = {date: new Date()}
    }
    /*挂载完毕*/
    componentDidMount() {
        this.timeID = setInterval(()=>{
            this.setState({
                date: new Date(),
            });
        },1000)
    }
    /*将要卸载*/
    componentWillUnmount() {
        clearInterval(this.timeID);
    }

    render() {
        return(
            <div>
                <h1>Hello World!</h1>
                <h2>现在是{this.state.date.toLocaleTimeString()}.</h2>
            </div>
        );
    }
}


ReactDOM.render(
    <Clock/>,
    document.getElementById('root')
);
